package cn.huanba.controller;



import cn.huanba.dto.Result;
import cn.huanba.utils.MathUtils;
import cn.huanba.utils.RedisUtils;
import cn.huanba.utils.ResultUtils;
import cn.huanba.utils.SmsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("sms")
@Api(value = "短信接口", tags = "短信接口", description = "发送短信")
public class SmsController {

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private RedisUtils redisUtils;

    //发送验证码
    @RequestMapping("/verifyCode")
    @ApiOperation(value = "短信", notes = "发送验证码")
    @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query", required = true, dataType = "string")
    public Result verifyCode(String phone) throws Exception {

        //生成随机数 验证码
        String code= MathUtils.random();
        boolean flag=false;
        if(redisUtils.set(phone,code,60*100))//保存到 redis 暂定100分钟
            //发送短信
            flag=smsUtils.sendMessage(phone,"SMS_197895260",code);

        return flag? ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

}
